package com.cuckoo.cuckoo_app2.adapters;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.cuckoo.cuckoo_app2.R;
import com.cuckoo.cuckoo_app2.apiAsync.Imp.UserApiImp;
import com.cuckoo.cuckoo_app2.apiAsync.UserApi;
import com.cuckoo.cuckoo_app2.dao.PlatList;
import com.cuckoo.cuckoo_app2.dao.Platform;
import com.cuckoo.cuckoo_app2.ui.dashboard.DashboardFragment;
import com.squareup.picasso.Picasso;

import java.util.List;

public class DashboadrdPlatformAdpter extends RecyclerView.Adapter<DashboadrdPlatformAdpter.ViewHolder> {

    private Context context;
    private List<Platform> dataList;
    private Drawable dr_red, dr_green;
    private int red, green;
    private DashboardFragment.ButtonClickListener buttonClickListener;

    public DashboadrdPlatformAdpter(Context context, List<Platform> dataList, DashboardFragment.ButtonClickListener buttonClickListener) {
        this.context = context;
        this.dataList = dataList;
        this.buttonClickListener = buttonClickListener;

        this.dr_red = ContextCompat.getDrawable(context, R.drawable.rounded_textview_red);
        this.dr_green = ContextCompat.getDrawable(context, R.drawable.rounded_textview_green);

        this.red = ContextCompat.getColor(context, R.color.red);
        this.green = ContextCompat.getColor(context, R.color.green);
    }

    @NonNull
    @Override
    public DashboadrdPlatformAdpter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dashboadrd_platform_compon, parent, false);
        return new DashboadrdPlatformAdpter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DashboadrdPlatformAdpter.ViewHolder holder, int position) {
        Platform platform = dataList.get(position);
        Log.i("TEST", "DashboadrdPlatformAdpter.onBindViewHolder：" + platform.toString());

        holder.pla_loginout.setVisibility(View.GONE);
        holder.pla_stopsin.setVisibility(View.GONE);
        holder.pla_login.setVisibility(View.GONE);
        holder.pla_startsin.setVisibility(View.GONE);
        holder.pla_enter.setVisibility(View.GONE);

        Picasso.get()
                .load(platform.getImageUrl())
                .placeholder(R.drawable.ic_launcher_foreground)
                .into(holder.pla_image);


        holder.pla_title.setText(platform.getTitle());
        holder.pla_account.setText(platform.getAccount());

        // 已登录 已接单
        // 已登录 未接单
        // 未登录 已接单
        // 未登录 未接单
        if (platform.getIsLogin().equals(1) && platform.getStart().equals(1)) {
            holder.pla_islogin.setText("已登录");
            holder.pla_islogin.setBackground(dr_green);
//            holder.pla_loginout.setVisibility(View.VISIBLE);

            holder.tips_start.setText("接单中");
            holder.tips_start.setTextColor(green);
            holder.pla_stopsin.setVisibility(View.VISIBLE);

            holder.pla_messag.setText(platform.getMessag());
            holder.pla_messag.setTextColor(green);

            holder.pla_enter.setVisibility(View.VISIBLE);
        } else if (platform.getIsLogin().equals(1) && platform.getStart().equals(0)) {
            holder.pla_islogin.setText("已登录");
            holder.pla_islogin.setBackground(dr_green);
//            holder.pla_loginout.setVisibility(View.VISIBLE);

            holder.tips_start.setText("暂停中");
            holder.tips_start.setTextColor(red);
            holder.pla_startsin.setVisibility(View.VISIBLE);

            holder.pla_messag.setText(platform.getMessag());
            holder.pla_messag.setTextColor(red);

            holder.pla_enter.setVisibility(View.VISIBLE);
        } else {
            holder.pla_islogin.setText("未登录");
            holder.pla_islogin.setBackground(dr_red);

            holder.tips_start.setText("暂停中");
            holder.tips_start.setTextColor(red);

            holder.pla_messag.setText(platform.getMessag());
            holder.pla_messag.setTextColor(red);

            holder.pla_login.setVisibility(View.VISIBLE);
        }

        // 绑定事件
        holder.pla_loginout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (buttonClickListener != null) {
                    buttonClickListener.onLoginout(platform);
                }
            }
        });
        // 绑定事件
        holder.pla_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (buttonClickListener != null) {
                    buttonClickListener.onLogin(platform);
                }
            }
        });
        // 绑定事件
        holder.pla_stopsin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (buttonClickListener != null) {
                    buttonClickListener.onStopsin(platform);
                }
            }
        });
        // 绑定事件
        holder.pla_startsin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (buttonClickListener != null) {
                    buttonClickListener.onStartsin(platform);
                }
            }
        });
        // 进入官方网站
        holder.pla_enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (buttonClickListener != null) {
                    buttonClickListener.onEnter(platform);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    // 清空数据并通知更新
    public void clearData() {
        this.dataList.clear(); // 假设 mData 不是 null
        notifyDataSetChanged();
    }

    // 更新数据列表的方法
    public void updateData(List<Platform> dataList) {
        this.dataList = dataList; // 直接替换数据列表
        notifyDataSetChanged(); // 通知RecyclerView数据已更改
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView pla_image;
        TextView pla_title;
        TextView pla_islogin;
        TextView pla_account;
        TextView tips_start;
        TextView pla_messag;
        Button pla_loginout;
        Button pla_stopsin;
        Button pla_login;
        Button pla_startsin;
        Button pla_enter;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            pla_image = itemView.findViewById(R.id.pla_image);
            pla_title = itemView.findViewById(R.id.pla_title);
            pla_islogin = itemView.findViewById(R.id.pla_islogin);
            pla_account = itemView.findViewById(R.id.pla_account);
            tips_start = itemView.findViewById(R.id.tips_start);
            pla_messag = itemView.findViewById(R.id.pla_messag);
            pla_loginout = itemView.findViewById(R.id.pla_loginout);
            pla_stopsin = itemView.findViewById(R.id.pla_stopsin);
            pla_login = itemView.findViewById(R.id.pla_login);
            pla_startsin = itemView.findViewById(R.id.pla_startsin);
            pla_enter = itemView.findViewById(R.id.pla_enter);
        }
    }

}
